package com.lungle.warmdiary.app.ui.guide;

import android.app.Activity;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ToggleButton;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.AppContext;
import com.lungle.warmdiary.app.utils.LogUtils;
import com.lungle.warmdiary.app.utils.TDevice;

/**
 * Created by zhangxueyuan on 16/9/28.
 * <p>
 * 点击球
 */
public class BallLayout extends FrameLayout {

    private View ball;
    private View menu;
    float xInView;
    float yInView;
    float xDownInScreen;
    float yDownInScreen;
    float xInScreen;
    float yInScreen;
    FrameLayout frameLayout;
    View linearLayout;

    public BallLayout(Activity context, AttributeSet attrs) {
        super(context, attrs);
        linearLayout = View.inflate(context, R.layout.view_change_release, this);

        frameLayout = (FrameLayout) context.getWindow().getDecorView();

        ball = linearLayout.findViewById(R.id.ball);
        menu = linearLayout.findViewById(R.id.change_menu);

        ToggleButton sw = (ToggleButton) linearLayout.findViewById(R.id.change_url_switch);

        View clear = linearLayout.findViewById(R.id.change_clear);
        View exit = linearLayout.findViewById(R.id.change_exit);
//        View close = linearLayout.findViewById(R.id.change_close);
        EditText editText = (EditText) linearLayout.findViewById(R.id.change_url_custom);
        View tb = linearLayout.findViewById(R.id.change_url_custom1);

        if (AppContext.getInstance().openCustom) {
            editText.setText(AppContext.getProperty(AppContext.CUSTOM));
        }

        tb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = editText.getText().toString();
                AppContext.getInstance().Logout();
                AppContext.getInstance().setOpenCustom(url);
                AppContext.showToast("请退出应用重新打开");

            }
        });

        if (AppContext.getInstance().openRelease)
            sw.setChecked(true);


        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                AppContext.getInstance().Logout();
                if (buttonView.isChecked()) {
                    AppContext.getInstance().setOpenRelease(true);
                    AppContext.showToast("使用线上地址,请退出应用重新打开");
                } else {
                    AppContext.getInstance().setOpenRelease(false);
                    AppContext.showToast("使用测试地址,请退出应用重新打开");
                }

            }
        });

        clear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AppContext.getInstance().setOpenCustom("");
                AppContext.getInstance().setOpenRelease(false);
                AppContext.getInstance().Logout();
                AppContext.getInstance().clearAppCache();
                AppContext.showToast("请退出应用重新打开！");
            }
        });

        exit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AppContext.getInstance().Logout();
                AppContext.showToast("请退出应用重新打开！");
            }
        });

        ball.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // 手指按下时记录必要数据,纵坐标的值都需要减去状态栏高度
                        xInView = event.getX();
                        yInView = event.getY();

                        xInScreen = event.getRawX();
                        yInScreen = event.getRawY() - TDevice.getStatuBarHeight();
                        xDownInScreen = xInScreen;
                        yDownInScreen = yInScreen;
                        LogUtils.printLogD(" " + xInScreen + " " + yInScreen + " " + xDownInScreen + " " + yDownInScreen);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        xInScreen = event.getRawX();
                        yInScreen = event.getRawY() - TDevice.getStatuBarHeight();
                        if (xDownInScreen != xInScreen || yDownInScreen != yInScreen) {

                            float x=xInScreen-xInView;
                            float y=yInScreen - yInView;
                            if(x<=0) {
                                linearLayout.setX(0);
                            }else{
                                linearLayout.setX(x);
                            }
                            if(y<=0){
                                linearLayout.setY(0);
                            }else{
                                linearLayout.setY(y);
                            }
                        }

                        break;
                    case MotionEvent.ACTION_UP:
                        LogUtils.printLogD(" " + xInScreen + " " + yInScreen + " " + xDownInScreen + " " + yDownInScreen);

                        if (xDownInScreen == xInScreen && yDownInScreen == yInScreen) {
                            if (menu.getVisibility() == VISIBLE) {
                                menu.setVisibility(GONE);
                            } else {
                                menu.setVisibility(VISIBLE);
                            }
                            return true;
                        }
//                        else {
//                            linearLayout.setX(xInScreen - xInView);
//                            linearLayout.setY(yInScreen - yInView);
//                        }
                        break;
                }
                return true;
            }
        });
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        frameLayout.addView(linearLayout, params);
    }


    public void remove(){
        if(frameLayout!=null&&linearLayout!=null){
            frameLayout.removeView(linearLayout);
        }
    }

}